package com.mate.cloud.xml.dtd;

import com.mate.cloud.xml.AbstractTestCase;
import org.dom4j.dtd.ExternalEntityDecl;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * Tests the {@link ExternalEntityDecl}functionality. Tests each of the
 * property access methods and the serialization mechanisms. Correct parsing is
 * tested by {@link DocTypeTest}.
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2024/6/3 15:08
 * @updateUser:     MI
 * @updateTime:     2024/6/3 15:08
 * @updateRemark:   修改内容
 * @version:        v1.0
 */
@SpringBootTest
public class ExternalEntityDeclTests extends AbstractTestCase {
    // Test case(s)
    // -------------------------------------------------------------------------
    @Test
    public void testToString() {
        ExternalEntityDecl decl1 = new ExternalEntityDecl("name", null,
                "systemID");
        ExternalEntityDecl decl2 = new ExternalEntityDecl("%name", null,
                "systemID");

        assertEquals("<!ENTITY name SYSTEM \"systemID\" >", decl1.toString());
        assertEquals("<!ENTITY % name SYSTEM \"systemID\" >", decl2.toString());
    }

    /**
     * Tests external entity declaration using only the SYSTEM identifier.
     */
    @Test
    public void testSystemId() {
        String expectedName = "anEntity";

        String expectedPublicID = null;

        String expectedSystemID = "http://www.myorg.org/foo";

        String expectedText = "<!ENTITY anEntity "
                + "SYSTEM \"http://www.myorg.org/foo\" >";

        ExternalEntityDecl actual = new ExternalEntityDecl(expectedName,
                expectedPublicID, expectedSystemID);

        assertEquals("name is correct", expectedName, actual.getName());

        assertEquals("publicID is correct", expectedPublicID, actual
                .getPublicID());

        assertEquals("systemID is correct", expectedSystemID, actual
                .getSystemID());

        assertEquals("toString() is correct", expectedText, actual.toString());
    }

    /**
     * Tests external entity declaration using both SYSTEM and PUBLIC
     * identifiers.
     */
    @Test
    public void testPublicIdSystemId() {
        String expectedName = "anEntity";

        String expectedPublicID = "-//dom4j//DTD sample";

        String expectedSystemID = "http://www.myorg.org/foo";

        String expectedText = "<!ENTITY anEntity "
                + "PUBLIC \"-//dom4j//DTD sample\" "
                + "\"http://www.myorg.org/foo\" >";

        ExternalEntityDecl actual = new ExternalEntityDecl(expectedName,
                expectedPublicID, expectedSystemID);

        assertEquals("name is correct", expectedName, actual.getName());

        assertEquals("publicID is correct", expectedPublicID, actual
                .getPublicID());

        assertEquals("systemID is correct", expectedSystemID, actual
                .getSystemID());

        assertEquals("toString() is correct", expectedText, actual.toString());
    }
}
